package com.lalosoft.barcodeDemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

/**
 * BarcodeDemo
 * Links utiles: 
 * -http://code.google.com/p/zxing/source/browse/trunk/androidtest/src/com/google/zxing/client/androidtest/ZXingTestActivity.java
 * -http://code.google.com/p/zxing/wiki/ScanningViaIntent
 * -http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/
 * 
 * Es necesario tener instalada la app BarcodeScanner.
 * -https://market.android.com/details?id=com.google.zxing.client.android
 * 
 * Importar el archivo ZXing.rar de la carpeta lib como libreria Android al proyecto.
 * 
 * @author lalosoft
 *
 */

public class BarcodeDemoActivity extends Activity {
    
	private static final String PACKAGE_NAME = "com.google.zxing.client.android";
	private static final String SCAN = PACKAGE_NAME + ".SCAN";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        Button scan = (Button) findViewById(R.id.scan_button);
        Button scanQR = (Button) findViewById(R.id.scanQR_button);
        Button scanAny = (Button) findViewById(R.id.scan_any_button);
        scan.setOnClickListener(scanProduct);
        scanQR.setOnClickListener(scanQRCode);
        scanAny.setOnClickListener(scanAnything);
    }
    
    public Button.OnClickListener scanProduct = new Button.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(SCAN);
            intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
            intent.setPackage(PACKAGE_NAME);
            startActivityForResult(intent, 0);
        }
    };
    
    public final Button.OnClickListener scanQRCode = new Button.OnClickListener() {
        public void onClick(View v) {
          Intent intent = new Intent("SCAN");
          intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
          startActivityForResult(intent, 0);
        }
      };
      
    public final Button.OnClickListener scanAnything = new Button.OnClickListener() {
       public void onClick(View v) {
         Intent intent = new Intent("SCAN");
         startActivityForResult(intent, 0);
       }
     };

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    	Log.e("ActivityResult: ", "INIT");
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan
                Log.e("ActivityResult: ", "Scan Contents: " + contents);
                Toast.makeText(this, "Scan contents: " + contents, Toast.LENGTH_LONG).show();
                Log.e("ActivityResult: ", "Scan Format: " + format);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }
}